﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.DataEntity;
using Cosmetic.Logic;
using DXBBS.Business;
using DXBBS.Components;
using DXBBS.DataProviders;
using DXBBS.Service;
using System.Reflection;

public partial class Controls_LoginState : System.Web.UI.UserControl
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!this.Request.Url.PathAndQuery.Contains("Login.aspx"))
		{
			//this.lnkLogin.HRef		= String.Format("~/Login.aspx?R={0}", this.Request.Url.PathAndQuery );
		}

		if (null == Session["SignInUser"])
        {
            this.txtPassword.Attributes.Add("onkeydown", "if(event.keyCode==13) {document.all." + this.btnSignIn.ClientID + ".focus();document.all." + this.btnSignIn.ClientID + ".click();}");
            this.txtUserName.Attributes.Add("onkeydown", "if(event.keyCode==13) {document.all." + this.btnSignIn.ClientID + ".focus();document.all." + this.btnSignIn.ClientID + ".click();}"); 
			this.pnlAnonymous.Visible	= true;
			this.pnlSignIn.Visible		= false;
		}
		else
		{
			this.pnlAnonymous.Visible	= false;
			this.pnlSignIn.Visible		= true;
			this.ltlNickname.Text		= (Session["SignInUser"] as User).NickName;
		}

	}

	protected void btnLogout_Click(object sender, EventArgs e)
	{
		Session.Remove("SignInUser");
		HttpCookie theCookie	= new HttpCookie("AutoSignIn");
		theCookie.Expires		= DateTime.Now.AddDays(-1);
		this.Response.Cookies.Add(theCookie);
		this.pnlAnonymous.Visible	= true;
		this.pnlSignIn.Visible		= false;

		//this.Response.Redirect(this.Request.Url.PathAndQuery);
	}

    protected void btnSignIn_Click(object sender, ImageClickEventArgs e)
    {
        UserManager theUserManger = new UserManager();
        Cosmetic.DataEntity.User theUser = theUserManger.LoginValidate(this.txtUserName.Text.Trim(), this.txtPassword.Text.Trim());
        if (theUser == null)
        {
            //Cosmetic.WebGeneric.WebMessageBox("用户不存在或密码错误!", this.Page);
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "Error", "alert('用户不存在或密码错误!');", true);
            Session.Remove("SignInUser");
        }
        else
        {
            Session["SignInUser"] = theUser;
            //this.pnlAnonymous.Visible	= false;
            //this.pnlSignIn.Visible		= true;
            //this.ltlNickname.Text		= theUser.NickName;

            //if (this.cbSavePassword.Checked)
            {
                HttpCookie theCookie = new HttpCookie("AutoSignIn");
                theCookie["Email"] = theUser.Email;
                theCookie["Password"] = theUser.Password;
                theCookie.Expires = DateTime.MaxValue;
                this.Response.Cookies.Add(theCookie);
                this.ltlNickname.Text = theUser.UserName;
            }

            this.pnlAnonymous.Visible = false;
            this.pnlSignIn.Visible = true;

            try
            {
                //sign in bbs
                Sessions.LoginTime = System.DateTime.Now; //Servers.DateTime;
                DXBBS.Components.UserInfo user = new DXBBS.Components.UserInfo();
                using (DataProvider provider = new DataProvider())
                {
                    user = DXBBS.Business.Forum.CheckUser(theUser.UserName, theUser.Password, true, provider);
                    if (user.ID != 0)
                    {
                        DXBBS.Business.Forum.UpdateTodayLogin(provider);
                        DXBBS.Business.Forum.AddCoinPoint(user.ID, ForumConfig.ReadConfigInfo().LoginCoin, ForumConfig.ReadConfigInfo().LoginPoint, provider);
                    }
                }
                if (user.ID == 0)
                {
                    //insert bbs user
                    user.UserName = theUser.UserName;
                    if (Cosmetic.WebGeneric.ReadBBSConfigInfo().PasswordType == 1)
                    {
                        user.UserPass = FormsAuthentication.HashPasswordForStoringInConfigFile(theUser.Password, "MD5").Substring(8, 0x10).ToLower();
                    }
                    if (Cosmetic.WebGeneric.ReadBBSConfigInfo().PasswordType == 2)
                    {
                        user.UserPass = FormsAuthentication.HashPasswordForStoringInConfigFile(theUser.Password, "MD5").ToLower();
                    }
                    if (Cosmetic.WebGeneric.ReadBBSConfigInfo().PasswordType == 3)
                    {
                        user.UserPass = FormsAuthentication.HashPasswordForStoringInConfigFile(FormsAuthentication.HashPasswordForStoringInConfigFile(theUser.Password, "MD5"), "MD5").ToLower();
                    }
                    if (Cosmetic.WebGeneric.ReadBBSConfigInfo().PasswordType == 4)
                    {
                        user.UserPass = FormsAuthentication.HashPasswordForStoringInConfigFile(theUser.Password, "SHA1").ToLower();
                    }
                    user.PasswordType = Cosmetic.WebGeneric.ReadBBSConfigInfo().PasswordType;
                    user.Email = theUser.Email;
                    user.NickName = theUser.NickName;
                    user.Question = "";
                    user.Answer = "";
                    //if (theUser.Sex == true)
                    //{
                    //    user.Sex = 1;
                    //    user.Photo = Cosmetic.WebGeneric.ReadBBSConfigInfo().MalePhoto;
                    //}
                    //else
                    //{
                    //    user.Sex = 2;
                    //    user.Photo = Cosmetic.WebGeneric.ReadBBSConfigInfo().FemalePhoto;
                    //}
                    user.PhotoWidth = Cosmetic.WebGeneric.ReadBBSConfigInfo().PhotoWidth;
                    user.PhotoHeight = Cosmetic.WebGeneric.ReadBBSConfigInfo().PhotoHeight;
                    if (Cosmetic.WebGeneric.ReadBBSConfigInfo().UserConfirm == 1)
                    {
                        user.State = 1;
                    }
                    user.UploadTime = Servers.DateTime;
                    user.LoginTime = Servers.DateTime;
                    user.RegTime = Servers.DateTime;
                    user.IP = Clients.IP;
                    user.LoginIP = Clients.IP;

                    using (DataProvider provider = new DataProvider())
                    {
                        DXBBS.Business.Forum.AddUser(user, provider);
                        Sessions.RegisterTime = System.DateTime.Now; //Servers.DateTime;
                        StatusInfo info3 = DXBBS.Business.Forum.ReadCacheStatusInfo();
                        info3.NewUser = user.UserName;
                        if (user.Sex == 0)
                        {
                            info3.TotalFemale++;
                        }
                        else
                        {
                            info3.TotalMale++;
                        }
                        if (Cosmetic.WebGeneric.ReadBBSConfigInfo().Interface == 1)
                        {
                            try
                            {
                                ((IUser)Assembly.Load("DxbbsUserClass").CreateInstance("DxbbsUserClass.Interface")).outRegister(user);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                if (user.State == 1)
                {
                    Informations.Deal(InformationType.Simple, LanguageKey.UserUnConfirm);
                }
                if (user.State == 2)
                {
                    Informations.Deal(InformationType.Simple, LanguageKey.UserLocked);
                }
                Sessions.IsHide = false;
                Hashtable hashtable = (Hashtable)Caches.Read(CacheKey.OnlineList);
                if (((hashtable != null) && (Sessions.UserID != 0)) && (hashtable[Sessions.UserID.ToString()] != null))
                {
                    hashtable.Remove(Sessions.UserID.ToString());
                }

                LevelInfo info2 = DXBBS.Business.Forum.ReadLevelCacheInfo(user.LevelID);
                Sessions.UserID = user.ID;
                Sessions.UserName = user.UserName;
                Sessions.Sex = user.Sex;
                Sessions.LevelID = user.LevelID;
                Sessions.LevelName = info2.LevelName;
                Sessions.LevelType = (LevelType)user.LevelType;
                Sessions.TeamID = user.TeamID;
                Sessions.Photo = user.Photo;
                Sessions.Topic = user.Topic;
                Sessions.Reply = user.Reply;
                Sessions.Coin = user.Coin;
                Sessions.Point = user.Point;
                Sessions.UploadNum = user.UploadNum;
                Sessions.UploadTime = user.UploadTime;
                Sessions.RegTime = user.RegTime;
            }
            catch
            { }
  

            //refresh welcome state
            //WebApp.iBeauty theMaster = (WebApp.iBeauty)this.Page.Master;
            //theMaster.ShowUser();
            //Response.Redirect(HttpContext.Current.Request.Url.ToString());

        }
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/Register/RegistAgreement.aspx");
    }

    protected void ImgBtnBasicInfo_Click(object sender, ImageClickEventArgs e)
    {
        if (Session["SignInUser"] != null)
        {
            Response.Redirect("~/Space/BasicInfo.aspx");
        }
        else
        {
            this.pnlAnonymous.Visible = true;
            this.pnlSignIn.Visible = false;
        }
    }

    protected void ImgBtnIntegral_Click(object sender, ImageClickEventArgs e)
    {
        if (Session["SignInUser"] != null)
        {
            Response.Redirect("~/Space/Integral.aspx");
        }
        else
        {
            this.pnlAnonymous.Visible = true;
            this.pnlSignIn.Visible = false;
        }
    }

    protected void ImgBtnFavorite_Click(object sender, ImageClickEventArgs e)
    {
        if (Session["SignInUser"] != null)
        {
            Response.Redirect("~/Space/Favorite.aspx");
        }
        else
        {
            this.pnlAnonymous.Visible = true;
            this.pnlSignIn.Visible = false;
        }
    }

    protected void ImgBtnComment_Click(object sender, ImageClickEventArgs e)
    {
        if (Session["SignInUser"] != null)
        {
            Response.Redirect("~/Space/Comments.aspx");
        }
        else
        {
            this.pnlAnonymous.Visible = true;
            this.pnlSignIn.Visible = false;
        }
    }

    protected void ImgBtnOrderInfo_Click(object sender, ImageClickEventArgs e)
    {
        if (Session["SignInUser"] != null)
        {
            Response.Redirect("~/Space/OrderRecord.aspx");
        }
        else
        {
            this.pnlAnonymous.Visible = true;
            this.pnlSignIn.Visible = false;
        }
    }

}
